var zTree =null;
var t;
var setting = {
    view: {
        dblClickExpand: false,
        showLine: false,
        selectedMulti: false
    },
    /*edit: {
        enable: true,
        showRenameBtn: false
    },*/
    check:{
        enable: true,
        chkboxType : { "Y" : "s", "N" : "" }
	},
    data: {
    	key:{
    		name:"permission_name"
		},
        simpleData: {
            enable:true,
            idKey: "id",
            pIdKey: "pId",
            rootPId: ""
        }
    },
    callback: {
        beforeClick: function(treeId, treeNode) {

        },
        onClick: function(){

        }
    }
};


$(document).ready(function(){
    t = $("#structureTree");
    t = $.fn.zTree.init(t, setting, getTreeData());
    zTree = $.fn.zTree.getZTreeObj("structureTree");
    var node = zTree.getNodeByParam("id", 1, null);
    var sn = zTree.selectNode(node);
    zTree.setting.callback.beforeClick("structureTree", node);//调用事件
    /*demoIframe = $("#testIframe");
    demoIframe.bind("load", loadReady);
    var zTree = $.fn.zTree.getZTreeObj("tree");
    zTree.selectNode(zTree.getNodeByParam("id",'11'));*/
});


var submitPermission = function(){
    var ids = "";
    var nodes = t.getCheckedNodes(true);
    nodes.forEach(function(node){
        ids +=node.id+",";
	});

    if(ids != ""){
        $.ajax({
            type:"post",
            url:basePath+"/role/editRolePer",
            data:{"perIds":ids,"roleId":$("#roleId").val()},
            async:false,
            success:function(data){
                if(data.code == "-3"){
                    layer.msg("管理员登录超时，请重新登录",{time:1000});
                    setTimeout(function(){
                        window.parent.parent.location.href=basePath+"/login";
                    },2000)
                    return false;
                }
                if(data.code=="0"){
                    layer.msg(data.msg,{icon:1,time:1000});
                }else{
                    alert('error');
                }
            }
        });
    }else{
        layer.msg('请更改数据',{icon:1,time:1000});
    }
}


var getTreeData = function(){
    var data = "";
    $.ajax({
        async:false,
        url:basePath+"/per/getTreePermission",
        data:{
            "roleId":$("#roleId").val()
        },
        success:function(resp){
            data = resp.data;
        }
    });

    return data;
}